package org.eclipse.soc.ide4edu.assignment.wizards;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Calendar;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class AssignmentDeadlineWizardPage extends WizardPage {

	/*--- SWT Widgets ---*/
	private DateTime date;
	private DateTime time;
	private Text maxMark;
	private Calendar deadline;
	/*--- End ---*/
	
	private InputStream deadlineDetailsStream;
	private int mark = 0;
	
	public AssignmentDeadlineWizardPage(ISelection select) {
		super("TeacherWizardPage3");
		setTitle("Assignment Deadline Wizard Page");
	}

	private void checkMarks() {
		try {
			if (maxMark.getText() != null)
				mark = Integer.parseInt(maxMark.getText());
		} catch (NumberFormatException e) {
			updateStatus("Not a valid Number");
		}
		updateStatus(null);

	}

	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gl = new GridLayout();
		composite.setLayout(gl);

		gl = new GridLayout();
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gl.numColumns = 2;

		Group deadlineGroup = new Group(composite, SWT.SHADOW_IN);
		deadlineGroup.setText("Deadline");
		deadlineGroup.setLayout(gl);
		deadlineGroup.setLayoutData(gd);

		new Label(deadlineGroup, SWT.BEGINNING).setText("Deadline Date: ");
		date = new DateTime(deadlineGroup, SWT.CALENDAR);

		new Label(deadlineGroup, SWT.BEGINNING).setText("Deadline Time: ");
		time = new DateTime(deadlineGroup, SWT.TIME);

		new Label(deadlineGroup, SWT.BEGINNING).setText("Maximum Mark");
		maxMark = new Text(deadlineGroup, SWT.BORDER | SWT.SINGLE);
		maxMark.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				checkMarks();
			}
		});

		setControl(composite);

	}

	private Calendar getDeadlineDate() {
		deadline = Calendar.getInstance();
		deadline.set(date.getYear(), date.getMonth(), date.getDay());
		return deadline;
	}

	public InputStream getDeadlineDetails() {
		deadline = getDeadlineDate();
		String deadlineDetails = new String();
		final String date = deadline.get(Calendar.DAY_OF_MONTH) + "-"
				+ deadline.get(Calendar.MONTH) + "-"
				+ deadline.get(Calendar.YEAR);
		final String time = getDeadlineTime();
		deadlineDetails += "Deadline Date:" + date + "\n";
		deadlineDetails += "Deadline Time:" + time + "\n";
		deadlineDetails += "Maximum Marks:" + getMaximumMark() + "\n";

		deadlineDetailsStream = new ByteArrayInputStream(deadlineDetails
				.getBytes());
		return deadlineDetailsStream;
	}

	private String getDeadlineTime() {
		final String t = time.getHours() + "-" + time.getMinutes();
		return t;
	}

	private int getMaximumMark() {
		if (maxMark.getText() == null)
			return 0;

		return mark;
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

}
